package vn.gq.tym.storytemplate;

import java.util.ArrayList;
import java.util.List;
import vn.gq.tym.model.CategoryAdapter;
import vn.gq.udv.db.items.Category;
import vn.gq.udv.utils.AdmobAdsHandler;
import vn.gq.udv.utils.Bookmark;
import vn.gq.udv.utils.ButtonHelper;
import vn.gq.udv.utils.SQLiteDatabaseVersionHandler;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.actionbarsherlock.view.MenuItem;
import com.google.ads.AdView;

public class CategoryActivity extends BaseActivity {

	public static final String PREF_STARTCATEGORYACTIVITY = "pref_startcategoryactivity";
	
	private ListView mListView;
	private TextView chapterNotification;
	private List<Category> mCategories = new ArrayList<Category>();
	private Bookmark bookmark;
	private AdmobAdsHandler mAdHandler;
	private CategoryAdapter mAdapter;
	private ProgressBar mProgressBar;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.chapter_scr);
		String author = getString(R.string.author_name);
		if (!author.trim().equals(""))
			getSupportActionBar().setSubtitle(author);
		new SQLiteDatabaseVersionHandler(this).checkDBVersionAndUpgrade();
		mListView = (ListView) findViewById(R.id.chapterlist);
		if (!GQConst.FINISHED) {
			ViewGroup vgr = (ViewGroup) ((LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.item_list_footer, null, false);
			mListView.addFooterView(vgr);
		}
		
		mProgressBar = (ProgressBar) findViewById(R.id.progress);
		
		chapterNotification = (TextView) findViewById(R.id.tvnotification);
		chapterNotification.setSelected(true);
		bookmark = new Bookmark(CategoryActivity.this, chapterNotification);
		
		mAdapter = new CategoryAdapter(CategoryActivity.this);
		mAdapter.load(new Runnable() {
			
			@Override
			public void run() {
				mListView.setAdapter(mAdapter);
				mCategories = mAdapter.getData();
				
				Log.i("longhoanggiang", "------------cat size: "+mCategories.size());
				
				mListView.setVisibility(View.VISIBLE);
				mProgressBar.setVisibility(View.GONE);
			}
		});
		
		
//		setListAdapter(new Runnable() {
//			
//			@Override
//			public void run() {
//				
//				if (mCategories.size() == 0) {
//					SharePreferenceUtil sharepref = new SharePreferenceUtil(CategoryActivity.this);
//					sharepref.putBoolean(PREF_STARTCATEGORYACTIVITY, false);
//					ChapterActivity.startActivity(CategoryActivity.this, 0, "");
//					finish();
//				}
//				
//				mListView.setAdapter(new ChapterItemAdapter(CategoryActivity.this, R.id.text_view, mCategories));
//				if (mProgressDlg != null) {
//					if (mProgressDlg.isShowing()) mProgressDlg.cancel();
//				}
//			}
//		});
//		mListView.setAdapter(new ChapterItemAdapter(this, R.id.text_view, mCategories));
		mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position,
					long id) {
				if (id != -1) {
					Category cat = mCategories.get(position);
					bookmark.setCategoryBookmark(cat);
					ChapterActivity.startActivity(CategoryActivity.this, cat.getCatId(), cat.getCatName());
				}
			}
		});
		mAdHandler = new AdmobAdsHandler((AdView) findViewById(R.id.adView));
		getSupportActionBar().setDisplayHomeAsUpEnabled(true);
	}

	@Override
	protected void onResume() {
		super.onResume();
		bookmark.showNotificationOnStartUp(true);
		mAdHandler.loadAds();
	}
	
	@Override
	public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu) {
		getSupportMenuInflater().inflate(R.menu.index, menu);
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch(item.getItemId()) {
			case android.R.id.home:
				onBackPressed();
				break;
			case R.id.menu_info:
				new ButtonHelper(CategoryActivity.this).showMoreApp();
				break;
		}
		return super.onOptionsItemSelected(item);
	}
	
	private class ChapterItemAdapter extends ArrayAdapter<Category> {

		private Context context;
		private List<Category> mChapterItems;
		
		public ChapterItemAdapter(Context context, int textViewResourceId,
				List<Category> objects) {
			super(context, textViewResourceId, objects);
			this.context = context;
			this.mChapterItems = objects;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			if (convertView == null) {
				convertView = ((LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.item_list, null, false);
			}
			Category item = this.mChapterItems.get(position);
			((TextView) convertView.findViewById(R.id.text_view)).setText(item.getCatName());
			convertView.setTag(item);
			return convertView;
		}
	}

	@Override
	protected void onStop() {
		mAdHandler.onStop();
		super.onStop();
	}

	@Override
	protected void onDestroy() {
		mAdHandler.onDestroy();
		super.onDestroy();
	}
}
